<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class Goodsattrcontroller extends Controller
{
	//后台商品属性添加页
    public function add()
    {
    	//查询根分类下的商品版块
    	$data = DB::table('cate') ->where('path','=','0') -> get();
    	// var_dump($data);
    	// die();
    	return view('admin.goodsattr.add',['title'=>'商品属性添加','data' => $data]);
    }

    //后台商品属性添加处理页
    public function insert(Request $request)
    {
    	$data = $request -> except('_token');
    	$data['catename'] = (DB::table('cate') -> where('id','=',$data['cid'])->first()-> name);
    	// var_dump($data); 
    	// die();

    	$res = DB::table('goodsattr') -> insert($data);

    	if($res){
    		return redirect('/admin/goodsattr/index') -> with(['success' => '添加成功']);
    	}else
    	{
    		return back() -> with(['error' => '添加失败']);
    	}
    }

    //后台商品列表页

 	public function index(Request $request)
    { 

    	$data = DB::table('goodsattr') -> get();
    	// var_dump($res);
    	// die();
    	$title = '商品属性列表页';
    	return view('admin.goodsattr.index',['title' => $title,'data' => $data]);
	}

	//edit
    public function edit($id)
    { 

    	$data = DB::table('goodsattr') 
    			-> where('id', '=', $id)
    			-> first();
    	$title = '商品属性修改页';		
    	// var_dump($data);
    	// exit();
    	return view('admin.goodsattr.edit',['title' => $title, 'data'=>$data]);		

    }

    //
    public function update(Request $request)
    {
    	$data = $request -> except('_token');

    	// var_dump($data);
    	// die();
    	$res = DB::table('goodsattr') -> where('id','=',$data['id']) -> update($data);

    	if($res){
    		return redirect('/admin/goodsattr/index') -> with(['success' => '修改成功']);
    	}else
    	{
    		return back() -> with(['error' => '修改失败']);
    	}
    }

    public function delete($id)
    {
    	$res = DB::table('goodsattr') -> where('id','=',$id) ->delete();
    	if($res){
    		return redirect('/admin/goodsattr/index') -> with(['success' => '删除成功']);
    	}else
    	{
    		return back() -> with(['error' => '删除失败']);
    	}
    }


}
